package com.nengchuan.eic.nceconomy.component.template;

import com.nengchuan.eic.nceconomy.constant.TemplateNameEnum;
import com.nengchuan.eic.nceconomy.entity.po.TemplateSheet;
import com.nengchuan.util.NullCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *施工图预算变电站增值税模板处理
 */
public class BudgetCalculationSubstationAvxHandler extends BaseHandler {
    //以下是需要验证的模板页
    private final static String[] SHEET_NAME_PREFIX={"变电总算表","建筑部分汇总表","安装部分汇总表","建筑单位工程汇总表","安装单位工程汇总表","其他费用表","建设场地征用及清理费用表"};

    private final static String[] SHEET_NAME_SUFFIX={"表一甲","表二乙","表二甲","表三乙","表三甲","表四","表七"};

    private final static int[] FIELD_IDX_END={2,3,3,3,3,2,2};

    private final static int[] LINE_INDEX_IDX_END={1,1,1,3,3,1,1};

    @Qualifier("templateHandlerSupport")
    @Autowired
    private HandlerSupport handlerSupport;


    @Override
    public List<ResponseModel> handlerRequest(RequestModel request) {
        String fileName=request.getFileName();

        List<ResponseModel> result=new ArrayList<>(SHEET_NAME_PREFIX.length);
        //如果文件名称包含关键字,则由本对象处理
        if (fileName.contains(TemplateNameEnum.PRODUCTION_DRAWING_BUDGET_SUBSTATION_ADDED_VALUE_TAX.text)) {
            Map<String,List<String[]>> data=request.getData();

            //遍历所有集合,key为sheetName value为sheet中的数据集合
            int order=0;
            for (Map.Entry<String, List<String[]>> e : data.entrySet()) {
                String sheetName = e.getKey();
                List<String[]> metadata = e.getValue();

                if(NullCheckUtils.isNull(sheetName)){
                    continue;
                }

                //下面循环为需要解析的sheet页
                ResponseModel response=new ResponseModel();
                for (int i = 0; i <SHEET_NAME_PREFIX.length ; i++) {
                    if (sheetName.contains(SHEET_NAME_PREFIX[i]) && sheetName.contains(SHEET_NAME_SUFFIX[i])) {
                        //根据sheetName模糊匹配模板sheetId,并解析创建sheet对象
                        TemplateSheet templateSheet = handlerSupport.createTemplateSheet(sheetName,order++);
                        response.setTemplateSheet(templateSheet);

                        handlerSupport.createTemplateDic(metadata, response,sheetName,FIELD_IDX_END[i],LINE_INDEX_IDX_END[i]);

                        result.add(response);
                    }
                }
            }
            return result;
        }
        //如果下级处理为空,则相应处理失败
        if(this.nextHandler==null){
            return null;
        }

        //交给下一级节点处理
        return this.nextHandler.handlerRequest(request);
    }
}
